/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.packet.client;

import lineage.controller.pc.L1PcController;
import lineage.core.L1GameClient;
import lineage.model.gameworld.L1World;
import lineage.model.instance.L1PcInstance;
import lineage.packet.L1AbstractPacketClient;
import lineage.packet.server.S_CharOriginalUpInfo;
import lineage.packet.server.S_CharPackOwn;
import lineage.packet.server.S_CharStatusOwn;
import lineage.packet.server.S_EnterToGame;
import lineage.packet.server.S_Light;
import lineage.packet.server.S_MapID;
import lineage.packet.server.S_PoisonStatus;
import lineage.packet.server.S_SPMR;
import lineage.packet.server.S_Weather;

/**
 * 处理客户端封包：请求进入游戏世界内.
 * 
 * @version 2014年3月16日下午1:26:47
 * @author jrwz
 */
public final class C_EnterToGame extends L1AbstractPacketClient {

    @Override
    public void execute(final L1GameClient client, final byte[] data) {
        try {
            this.init(data);

            if (client.getOnlineChar() != null) {
                return;
            }

            final String name = readS();

            final boolean isOnline = L1PcController.isOnline(name); // 检查是否在线
            boolean isAdd = false;
            if (isOnline) {
                return;
            } else {
                isAdd = L1PcController.addOnline(name); // 加入在线列表
            }
            if (!isAdd) {
                return;
            }

            final L1PcInstance pc = L1PcController.getPc(name);
            if (pc == null) {
                return;
            }

            client.setOnlineChar(pc); // 设置为在线角色

            pc.setStatus(4);
            pc.setNetConnection(client);
            pc.sendPackets(new S_EnterToGame()); // 宣告进入游戏
            pc.sendPackets(new S_CharStatusOwn(pc)); // 角色资讯(自身)
            pc.sendPackets(new S_MapID(pc)); // 更新角色所在的地图
            pc.sendPackets(new S_CharPackOwn(pc)); // 发送自身封包
            pc.sendPackets(new S_SPMR(pc)); // 更新魔攻魔防
            pc.sendPackets(new S_CharOriginalUpInfo(pc)); // 发送初始能力加成信息
            pc.sendPackets(new S_PoisonStatus(pc)); // 发送中毒状态外观效果
            pc.sendPackets(new S_Light(pc)); // 自身照明范围
            pc.sendPackets(new S_Weather(S_Weather.SNOW)); // 发送游戏天气

            L1World.getInstance().addObject(pc); // 加入世界
            L1World.getInstance().addVisibleObject(pc); // 加入世界(区分地图)
            pc.updateObject(); // 更新可见对象
        } catch (final Exception e) {
        } finally {
            this.close();
        }
    }
}
